package com.qufadai.mvc.context;

import javax.servlet.http.HttpServletResponse;

import com.qufadai.mvc.log.LogFactory;
import com.qufadai.mvc.log.Logger;
import com.qufadai.mvc.util.JsonUtil;

public abstract class ResponseContext {
	private static Logger logger = LogFactory.getDefaultLog();

	/**
	 * 重定向
	 * 
	 * @param url
	 */
	public static void sendRedirect(String url) {
		ContextHolder.sendRedirect(url);
	}

	/**
	 * 转到相应的inner进行处理
	 * 
	 * @param innerResource
	 */
	public static void forword(String innerResource) {
		ContextHolder.forward(innerResource);
	}

	/**
	 * 返回json数据
	 * 
	 * @param obj
	 */
	public static void responseJson(Object obj) {
		String json = JsonUtil.parseToJson(obj);
		logger.log(json);
		ContextHolder.response(json);
	}

	/**
	 * 返回一个简单的字符串
	 * 
	 * @param content
	 */
	public static void responseText(String content) {
		ContextHolder.response(content);
	}

	/**
	 * 返回容器初始的response对象
	 * 
	 * @return
	 */
	public static HttpServletResponse getCommonResponse() {
		return ContextHolder.findInstance().getResponse();
	}

	/**
	 * 设置用于返回的对象
	 * 
	 * @param key
	 * @param value
	 */
	public static void setReturnAttr(String key, Object value) {
		ContextHolder.setRequestAttribute(key, value);
	}
}
